#[macro_use]
extern crate maplit;

use std::{collections::HashMap, sync::Arc};

use hyper::Response;
use web::Engine;
use web::{prelude::*, router::Router};

#[tokio::main]
pub async fn main() -> Result {
    simple_logger::SimpleLogger::new().init()?;

    let mut router = Router::new();

    router.get("/", |req| {
        Response::builder()
            .status(200)
            .html("<h1>Hello World</h1>")
            .unwrap()
    });

    router.get("/hello", |req| {
        Response::builder()
            .status(200)
            .string(format!(
                "hello {}, you're at {}\n",
                req.query("name").unwrap(),
                req.uri().path()
            ))
            .unwrap()
    });

    router.post("/login", |req| {
        let data = req.json::<HashMap<String, String>>();
        Response::builder().status(200).json(data).unwrap()
    });

    let engine = Engine::new(router);
    Arc::new(engine).run("127.0.0.1:9999".parse()?).await?;

    Ok(())
}
